package org.example.device.control;

import org.example.device.protocol.DeviceProtocol;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 智能温控器控制器
 * 实现智能温控器的控制逻辑
 */
@Component
public class SmartThermostatController extends AbstractDeviceController {
    
    private static final String DEVICE_TYPE = "SMART_THERMOSTAT";
    private static final List<String> SUPPORTED_COMMANDS = Arrays.asList(
        "temperature", "mode", "fan_speed", "schedule", "power"
    );
    
    @Override
    public Map<String, Object> getDeviceProperties(Long deviceId) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("deviceType", DEVICE_TYPE);
        properties.put("supportedCommands", SUPPORTED_COMMANDS);
        properties.put("minTemperature", 16);
        properties.put("maxTemperature", 30);
        properties.put("supportedModes", Arrays.asList("cool", "heat", "auto", "fan", "dry"));
        properties.put("fanSpeeds", Arrays.asList("low", "medium", "high", "auto"));
        properties.put("hasSchedule", true);
        return properties;
    }
    
    @Override
    public DeviceControlResult setDeviceProperties(Long deviceId, Map<String, Object> properties) {
        try {
            // 验证属性
            if (!validateThermostatProperties(properties)) {
                return DeviceControlResult.failure("温控器属性验证失败", "INVALID_PROPERTIES");
            }
            
            // 构建控制命令
            DeviceControlCommand command = new DeviceControlCommand(deviceId, "properties", properties);
            return executeCommand(command);
        } catch (Exception e) {
            return DeviceControlResult.failure("设置温控器属性失败: " + e.getMessage());
        }
    }
    
    @Override
    public List<String> getSupportedCommands(Long deviceId) {
        return new ArrayList<>(SUPPORTED_COMMANDS);
    }
    
    @Override
    public String getDeviceType() {
        return DEVICE_TYPE;
    }
    
    /**
     * 智能温控器使用WebSocket协议
     */
    @Override
    protected DeviceProtocol getDeviceProtocol() {
        return protocolRegistry.getProtocol("WEBSOCKET");
    }
    
    /**
     * 验证温控器属性
     */
    private boolean validateThermostatProperties(Map<String, Object> properties) {
        if (properties == null) return false;
        
        // 验证温度
        if (properties.containsKey("temperature")) {
            Object temp = properties.get("temperature");
            if (temp instanceof Number) {
                double value = ((Number) temp).doubleValue();
                if (value < 16 || value > 30) {
                    return false;
                }
            }
        }
        
        // 验证模式
        if (properties.containsKey("mode")) {
            Object mode = properties.get("mode");
            if (!(mode instanceof String)) {
                return false;
            }
            List<String> validModes = Arrays.asList("cool", "heat", "auto", "fan", "dry");
            if (!validModes.contains(mode)) {
                return false;
            }
        }
        
        // 验证风速
        if (properties.containsKey("fanSpeed")) {
            Object fanSpeed = properties.get("fanSpeed");
            if (!(fanSpeed instanceof String)) {
                return false;
            }
            List<String> validSpeeds = Arrays.asList("low", "medium", "high", "auto");
            if (!validSpeeds.contains(fanSpeed)) {
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * 控制温度
     */
    public DeviceControlResult controlTemperature(Long deviceId, double temperature, Long userId) {
        if (temperature < 16 || temperature > 30) {
            return DeviceControlResult.failure("温度值必须在16-30度之间", "INVALID_TEMPERATURE");
        }
        
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "temperature", temperature);
        command.setUserId(userId);
        command.setAction("温度控制");
        return executeCommand(command);
    }
    
    /**
     * 控制模式
     */
    public DeviceControlResult controlMode(Long deviceId, String mode, Long userId) {
        List<String> validModes = Arrays.asList("cool", "heat", "auto", "fan", "dry");
        if (!validModes.contains(mode)) {
            return DeviceControlResult.failure("无效的模式", "INVALID_MODE");
        }
        
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "mode", mode);
        command.setUserId(userId);
        command.setAction("模式控制");
        return executeCommand(command);
    }
    
    /**
     * 控制风速
     */
    public DeviceControlResult controlFanSpeed(Long deviceId, String fanSpeed, Long userId) {
        List<String> validSpeeds = Arrays.asList("low", "medium", "high", "auto");
        if (!validSpeeds.contains(fanSpeed)) {
            return DeviceControlResult.failure("无效的风速", "INVALID_FAN_SPEED");
        }
        
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "fan_speed", fanSpeed);
        command.setUserId(userId);
        command.setAction("风速控制");
        return executeCommand(command);
    }
    
    /**
     * 设置定时任务
     */
    public DeviceControlResult setSchedule(Long deviceId, Map<String, Object> schedule, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "schedule", schedule);
        command.setUserId(userId);
        command.setAction("定时任务设置");
        return executeCommand(command);
    }
}
